package cn.doourbest.learnspring.tostring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: zhangsc
 * @create: 2019/11/8
 */
public class MyTOStringBO {

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                sb.append(field.getName()).append(":").append(field.get(this)).append(",");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 去掉多于的逗号
        if(sb.length() > 1){
            sb.deleteCharAt(sb.length()-1);
        }
        sb.append("]");
        return sb.toString();
    }


    public static void main(String[] args) {
        List<MyBO> list = new ArrayList<>();
        MyBO myBO = new MyBO();
        myBO.setInteger(1);
        myBO.setLlong(2L);
        myBO.setName("zsc1");
        list.add(myBO);

        myBO = new MyBO();
        myBO.setInteger(1);
        myBO.setLlong(2L);
        myBO.setName("zsc1222");
        list.add(myBO);


        list.stream().map(MyBO::getName).forEach(System.out::println);

    }
}
